#include <Arduino.h>
#include "taskAPP.h"
#include <WiFi.h>
#include <WebServer.h>

// ------------------------------------------------------
const char *ssid = "FAST_A618";     //你的网络名称
const char *password = "123456789"; //你的网络密码

void WifiInit()
{
    Serial.begin(9600);
    Serial.println();

    WiFi.begin(ssid, password);           //连接网络
    while (WiFi.status() != WL_CONNECTED) //等待网络连接成功
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println("WiFi connected!");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP()); //打印模块IP
}
// --------------------------------------------------------

unsigned char s;

WebServer server(5000);
void OnLed21() //回调函数
{
    digitalWrite(led21, 1);
    s = 0;
    String key = String(s);
    server.send(200, "text/plain", key);
}

void OffLed21() //回调函数
{
    digitalWrite(led21, 0);
    s = 1;
    String key = String(s);
    server.send(200, "text/plain", key);
}

void TaskServer(void *pr) // task webserver for led21
{
    pinMode(led21, OUTPUT);
    digitalWrite(led21, 1);

    server.on("/on", OnLed21);
    server.on("/off", OffLed21);
    server.begin();

    while (1)
    {
        server.handleClient();
        Serial.println("led21 is blinking");
        vTaskDelay(200);
    }
}

void TaskLed22(void *pr)
{
    pinMode(led22, OUTPUT);
    unsigned char s = 0;
    while (1)
    {
        digitalWrite(led22, s);
        s = ~s;
        Serial.println("led22 is blinking");
        vTaskDelay(600);
    }
}
